# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pygments

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.6
pkgrel=3
pkgdesc="Python syntax highlighter (mingw-w64)"
arch=('any')
license=('BSD')
url="http://pygments.org/"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools" "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
source=(http://pypi.python.org/packages/source/P/Pygments/Pygments-$pkgver.tar.gz)
sha256sums=('799ed4caf77516e54440806d8d9cd82a7607dfdf4e4fb643815171a4b5c921c0')
 
build() {
  cd "$srcdir"

  rm -rf python{2,3}-build
  for builddir in python{2,3}-build; do
    cp -r Pygments-$pkgver $builddir
    pushd $builddir
    ${MINGW_PREFIX}/bin/${builddir%-build} setup.py build
    popd
  done
}

package_python3-pygments() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools")

  pushd ${MINGW_PREFIX} > /dev/null
  local _mingw_prefix=`pwd -W`
  popd > /dev/null

  cd "$srcdir/python3-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="$pkgdir" -O1

  # pygmentize has been moved to the python2-pygments package
  #rm ${pkgdir}${MINGW_PREFIX}/bin/pygmentize.exe{,.manifest}
  #rm -rf ${pkgdir}${MINGW_PREFIX}/bin

  for f in pygmentize; do
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe "${pkgdir}${MINGW_PREFIX}"/bin/${f}-3.4.exe
    if [ -f "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest ]; then
      mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest "${pkgdir}${MINGW_PREFIX}"/bin/${f}-3.4.exe.manifest
      sed -e "s|${f}|${f}-3.4|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${f}-3.4.exe.manifest
    fi
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}-script.py "${pkgdir}${MINGW_PREFIX}"/bin/${f}-3.4-script.py
  done
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_f}
  done
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
}
 
package_python2-pygments() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools")

  pushd ${MINGW_PREFIX} > /dev/null
  local _mingw_prefix=`pwd -W`
  popd > /dev/null

  cd "$srcdir/python2-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="$pkgdir" -O1

  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_f}
  done
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"
}

package_mingw-w64-i686-python2-pygments() {
  install=pygments2-${CARCH}.install
  package_python2-pygments
}

package_mingw-w64-i686-python3-pygments() {
  install=pygments3-${CARCH}.install
  package_python3-pygments
}

package_mingw-w64-x86_64-python2-pygments() {
  install=pygments2-${CARCH}.install
  package_python2-pygments
}

package_mingw-w64-x86_64-python3-pygments() {
  install=pygments3-${CARCH}.install
  package_python3-pygments
}
